<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:fmt="http://java.sun.com/jsp/jstl/fmt" xmlns:c="">
<head>
    <meta charset="UTF-8">
    <title>PetStore Management</title>
<!--    <link rel="Stylesheet" href="../css/jpetstore.css" type="text/css" media="screen"/>-->
    <script type="text/javascript" src="../js/jquery-3.6.0.min.js"></script>
    <!--导入jquery-ui-->
    <script type="text/javascript" src="../js/jquery-ui.min.js"></script>
    <link type="text/css" href="../js/jquery-ui.min.css" rel="stylesheet">
</head>
<style>
    *{
        padding: 0px;
        margin: 0px;
    }
    #Content {
        width: 100%;
        height: 110ex;
        color: #333;
        background-color: rgba(256,256,256,0.5);
        border-width: 0;
        position: relative;
        overflow: hidden;
    }
    a{
        display: block;
        text-decoration: none;
        color: #eaac00;
        font-weight: bold;
    }
    .Returnto{
        display: inline-block;
        float: left;
        position: absolute;
        top: 1ex;
        left: 2ex;
        height: 5ex;
        width: 50ex;
        background-image: url("../image/ReturntoManagementMainMenu.png");
        background-size: 100%;
        background-repeat: no-repeat;
        border-color: #eaac00;
        border-width: 0.4ex;
        border-style: solid;
    }
    table{
        display: block;
        /*position: absolute;*/
        /*top: 10ex;*/
        /*left: 10ex;*/
        margin-top: 10%;
        margin-left: 32%;
        width: 45ex;
        font-size: 3ex;
        background-color: #ffffff;
        border-color: #56832c;
        border-width: 1ex;
        border-style: solid;
    }
    table tr{
        height: 4ex;
        text-align: left;
    }
    table td{
        width: 10ex;
    }
    #search {
        width: 40%;
        height: 7ex;
        background-color: #ffffff;
        border-color: #56832c;
        border-width: 0.7ex;
        border-style: solid;
        text-align: center;
        color: #eaac00;
        position: absolute;
        display: inline-block;
        top: 1ex;
        left: 57ex;
    }

    #search input {
        float: left;
        position: absolute;
        border-width: .1ex .1ex .1ex .1ex;
        border-style: solid;
        border-color: #eaac00;
        background-color: #ffffff;
        color: #eaac00;
        height: 3ex;
        font-size:3ex;
    }
    input{
        border-width: .2ex;
        border-style: solid;
        border-color: #eaac00;
        background-color: #ffffff;
        color: #eaac00;
        height: 3ex;
    }
    #search span{
        display: inline-block;
        float: left;
        position: absolute;
        font-size: 3ex;
    }
    h2{

        float: left;
        position: absolute;
        top: 5ex;
        left: 33ex;
        font-size: 5ex;
    }

    #add{
        position: absolute;
        display:inline-block;
        background-image: url("../image/ADD.png");
        background-size: 100%;
        background-repeat: no-repeat;
        width: 20ex;
        height: 9ex;
        top: 100ex;
        left: 97ex;
        background-color: rgba(256,256,256,0);
    }
    #return{
        position: absolute;
        display:inline-block;
        background-image: url("../image/RETURN.png");
        background-size: 100%;
        background-repeat: no-repeat;
        width: 15ex;
        top: 110ex;
        left: 2ex;
        height: 5ex;
    }
</style>
<body>
<div th:replace="manageCommon/managetop"></div>
</div>

<div id="Content">
    <a class="Returnto" href="/manage/main"></a>
    <div id="search">
        <div id="searchContent">
            <!--搜索-->
            <form action="" method="post" id="keyForm">
                <!--右侧搜索选择复选框-->
                <span style="left: 1ex;top: 1ex ">category:</span>
                <input style="left: 9.5ex;top: 0.8ex;width: 2ex" type="radio" name="select" id="category"/>
                <span style="left: 13ex;top: 1ex">product:</span>
                <input style="left: 20ex;top: 0.9ex;width: 2.1ex" type="radio" name="select" id="product"/>
                <!--开启自动补全功能autocomplete="on"-->
                <input  style="left: 25ex;top: 0.6ex;width: 18ex"id="searchText" type="text" name="keyword" size="14" />
                <input style="left: 44ex;top: 1ex;width: 7ex" type="button" name="searchProducts" value="Search" id="searchSubmit"/>
            </form>
        </div>
    </div>
    <div>
        <form th:id="newItemForm" action="" method="post">
            <h2>Add Item Information</h2>
            <table>
                <tr>
                    <td>Product ID: </td>
                    <td>
                        <input id="produ1" list="productId" name="productIdSelect" />
                        <datalist id="productId" >
                            <option th:each="productId:${session.allProductId}" th:text="${productId}" th:value="${productId}"></option>
                        </datalist>
                    </td>
                </tr>
                <tr>
                    <td>ListPrice: </td>
                    <td>
                        <input name="listprice" id="listprice" />
                    </td>
                </tr>
                <tr>
                    <td>UnitCost: </td>
                    <td>
                        <input name="unitcost" id="unitcost" />
                    </td>
                </tr>
                <tr>
                    <td>SupplierId: </td>
                    <td>
                        <select name="supplier" id="supplier">
                            <option value="1" selected="selected">1</option>
                            <option value="2">2</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Status: </td>
                    <td name="status" text="P"></td>
                </tr>
                <tr>
                    <td>Attribute1: </td>
                    <td>
                        <input name="attr1" id="attr1"/>
                    </td>
                </tr>
                <tr>
                    <td>Attribute2: </td>
                    <td>
                        <input name="attr2" id="attr2"/>
                    </td>
                </tr>
                <tr>
                    <td>Attribute3: </td>
                    <td>
                        <input name="attr3" id="attr3"/>
                    </td>
                </tr>
                <tr>
                    <td>Attribute4: </td>
                    <td>
                        <input name="attr4" id="attr4"/>
                    </td>
                </tr>
                <tr>
                    <td>Attribute5: </td>
                    <td>
                        <input name="attr5" id="attr5"/>
                    </td>
                </tr>
            </table>
        </form>
        <input id="add" type="button"  style="display:block;" th:onclick="add()"/>
        <input id="return" type="button"  th:onclick="backItemList('[[${session.beforeProductId}]]')"/>
    </div>
    <script>
        $(function (){
            //对搜索输入框的键盘按下后的事件进行监听keyup
            $('#searchText').on("keyup",function (){
                let datas={"word":$('#searchText').val()};
                $.post('/catalog/completeSearch',datas,function (data){

                });
                $.ajax({
                    //连接数据库进行查询
                    type:"GET",
                    url:"/catalog/completeSearch",
                    data:{"keyword":$('#searchText').val()},
                    dataType:"json",
                    success:function (data){
                        console.log("888888888888");
                        //创建数组用于存放获取的产品的名字
                        var searchResult=new Array();
                        for(var i=0;i<data.length;i++){
                            searchResult[i]=data[i].name;
                        }
                        //console.log(searchResult);
                        //将产品名显示在UI控件中
                        $('#searchText').autocomplete({
                            source:searchResult
                        });
                    }
                });
            });
            $('#searchSubmit').on('click',function (e) {
                e.preventDefault();
                console.log("点击搜索按钮");
                let datas = $('#keyForm').serialize();
                $.post('/catalog/searchProduct',datas,function (data){
                    if(data.code === 11)
                    {
                        console.log("搜索成功");
                        location.href = data.msg;
                    }

                });
            });
        });
        function add(){
            let datas = $('#newItemForm').serialize();
            if(document.getElementById("produ1").value.length===0||document.getElementById("produ1").value===null){
                window.alert("productId不可为空");
            }else{
                var attr1=document.getElementById("attr1").value;
                var attr2=document.getElementById("attr2").value;
                var attr3=document.getElementById("attr3").value;
                var attr4=document.getElementById("attr4").value;
                var attr5=document.getElementById("attr5").value;
                if((attr1!==null&&attr1.length!==0)||(attr2!==null&&attr2.length!==0)||(attr3!==null&&attr3.length!==0)||
                (attr4!==null&&attr4.length!==0)||(attr5!==null&&attr5.length!==0)){
                    if(document.getElementById("listprice").value.length===0||document.getElementById("listprice").value===null){
                        window.alert("请将listprice填写完整");
                    }else if(document.getElementById("unitcost").value.length===0||document.getElementById("unitcost").value===null){
                        window.alert("请将unitcost填写完整");
                    }
                    $.post('/manage/addItem',datas,function (data){
                        if(data.code===1){
                            console.log("123");
                            location.href=data.msg;
                        }
                    });
                }else{
                    window.alert("attr1-5至少填写一项");
                }
            }
        }
        function backItemList(beforProductId){
            var str=beforProductId.split('"');
            location.href="/manage/intoItemPage?productid="+str[1];
        }
    </script>
</div>
<div th:replace="manageCommon/managebottom"></div>
</body>
</html>